//如果二叉树每个节点都具有相同的值，那么该二叉树就是单值二叉树。 
//
// 只有给定的树是单值二叉树时，才返回 true；否则返回 false。 
//
// 
//
// 示例 1： 
//
// 
//
// 输入：[1,1,1,1,1,null,1]
//输出：true
// 
//
// 示例 2： 
//
// 
//
// 输入：[2,2,2,5,2]
//输出：false
// 
//
// 
//
// 提示： 
//
// 
// 给定树的节点数范围是 [1, 100]。 
// 每个节点的值都是整数，范围为 [0, 99] 。 
// 
// Related Topics 树 深度优先搜索 广度优先搜索 二叉树 👍 110 👎 0

package leetcode.editor.cn;

import java.util.ArrayDeque;
import java.util.Deque;

public class _965_UnivaluedBinaryTree {

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public static void main(String[] args) {
        Solution solution = new _965_UnivaluedBinaryTree().new Solution();
//        TreeNode root = new TreeNode(1,
//                new TreeNode(1, new TreeNode(1), new TreeNode(1)),
//                new TreeNode(1, null, new TreeNode(1)));
        TreeNode root = new TreeNode(2,
                new TreeNode(2, new TreeNode(2), new TreeNode(5)),
                new TreeNode(2));
        System.out.println(solution.isUnivalTree(root));
    }

    class Solution {
        public boolean isUnivalTree(TreeNode root) {
            if (root == null) {
                return true;
            }
            if (root.left != null) {
                if (root.val != root.left.val || !isUnivalTree(root.left)) {
                    return false;
                }
            }
            if (root.right != null) {
                if (root.val != root.right.val || !isUnivalTree(root.right)) {
                    return false;
                }
            }
            return true;
        }
    }

    class Solution2 {
        public boolean isUnivalTree(TreeNode root) {
            int val = root.val;
            Deque<TreeNode> stack = new ArrayDeque<>();
            TreeNode cur = root;
            while (!stack.isEmpty() || cur != null) {
                while (cur != null) {

                    stack.push(cur);
                    cur = cur.left;
                }
                cur = stack.pop();
                if (cur.val != val) return false;
                cur = cur.right;
            }
            return true;
        }
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     * int val;
     * TreeNode left;
     * TreeNode right;
     * TreeNode() {}
     * TreeNode(int val) { this.val = val; }
     * TreeNode(int val, TreeNode left, TreeNode right) {
     * this.val = val;
     * this.left = left;
     * this.right = right;
     * }
     * }
     */
    class Solution1 {
        public boolean isUnivalTree(TreeNode root) {
            int val = root.val;
            Deque<TreeNode> stack = new ArrayDeque<>();
            stack.push(root);
            TreeNode cur;
            while (!stack.isEmpty()) {
                cur = stack.pop();
                if (cur.val != val) return false;
                if (cur.left != null) stack.push(cur.left);
                if (cur.right != null) stack.push(cur.right);
            }
            return true;
        }

    }
//leetcode submit region end(Prohibit modification and deletion)

}